package com.newland.zxy;

import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

@Service
public class RedisAnnotationService {
    /**
     * 将结果放入redis缓存
     * @param user
     * @return
     */
    @CachePut(value = "rediscache", key = "'user_'+#result.id",condition = "#result!='null'")
    public User insertUser(User user)
    {
        System.out.println("cache put------------------");
        return user;
    }

    /**
     * 先从缓存中查找，若无结果，执行函数，将函数结果返回并将结果放入缓存
     * @param id
     * @return
     */
    @Cacheable(value = "rediscache", key="'user_'+#id",condition = "#result!='null'")
    public User getUser(Integer id)
    {
        System.out.println("get user-------------------------------");
        return new User(id,"he");
    }

    /**
     * 缓存失效
     * @param id
     */
    @CacheEvict(value = "rediscache", key="'user_'+#id")
    public void deleteUser(Integer id)
    {
        System.out.println("delete user----------------------------");
    }
}
